package com.ghyf.mplay.application;



import com.ghyf.mplay.bean.BaseModuleInfo;
import com.ghyf.mplay.bean.GlobalParamInfo;

import com.ghyf.mplay.util.LogUtil;
import com.ghyf.mplay.util.StorageUtil;
import com.ghyf.mplay.value.ConstantValue;

import android.app.AlarmManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.os.Build;
import android.util.SparseArray;
/**
 * 
 * Application类
 * @author cuiran
 * @version 1.0.0
 */
public class BaseCookie extends Application implements ConstantValue{

	
	private static final String TAG="BaseCookie";
	private static final String SYSSHARED="sysShared";
    private static BaseCookie instance;
	/**
	 * 日志服务
	 */
	private Intent logService = new Intent("com.ghyf.service.LOG");//LogService的action,看AndroidManifest.XML
	
	public static SharedPreferences.Editor sysEdit;
	public static SharedPreferences sysShare;
	/**
	 * 性能优化之使用SparseArray替代HashMap
	 * 用来存放UID和对应Module
	 */
	public static SparseArray<BaseModuleInfo> moduleMap=new SparseArray<BaseModuleInfo>();
	
	/**
	 * 创建方法
	 */
	public void onCreate() {
		super.onCreate();
		try{
			
			
			sysShare = getSharedPreferences(SYSSHARED, 0);
			sysEdit = sysShare.edit();
			/**
			 * 自定义系统的Crash捕捉类，用Toast替换系统的对话框 
			 */
			CustomCrashHandler mCustomCrashHandler = CustomCrashHandler.getInstance();
			mCustomCrashHandler.setCustomCrashHanler(getApplicationContext());
			
			/**
			 * 设置播表所在的路径
			 */
			//YGPlaylist122   playlist102 playlist102 playlist201
//			if(LAYOUT_TYPE_DEFAULT.equals(layout_type_1080i)){
//				UserInfoSingleton.setPlayPath(ConstantValue.ROOT_DIR + "playlist202/");
//			}else if(LAYOUT_TYPE_DEFAULT.equals(layout_type_720p)){
//				UserInfoSingleton.setPlayPath(ConstantValue.ROOT_DIR + "YGPlaylist122/");
//			}
//			UserInfoSingleton.setPlayPath(ConstantValue.ROOT_SDCARD_DIR + "YGPlaylist106/");
			if(null==UserInfoSingleton.getPlayPath()||"".equals(UserInfoSingleton.getPlayPath())){
				UserInfoSingleton.setPlayPath(ConstantValue.ROOT_DIR + GlobalParamInfo.getInstance().getRoot_dir()+"/");
			}
			
//			UserInfoSingleton.setPlayPath(ConstantValue.ROOT_SDCARD_DIR + "GFYGPlaylist401/");
			UserInfoSingleton.setIslive(ConstantValue.PLAY_LOCAL);
			UserInfoSingleton.putBooleanAndCommit("isStop", false);
			//设置时区
			AlarmManager mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
			mAlarmManager.setTimeZone("Asia/Shanghai");
	
			instance=this;
			
			startService(logService);
			LogUtil.i(TAG,"日志服务启动");

			
		}catch (Exception e) {
			
			LogUtil.e(TAG, "出现异常",e);
		}
	
		
	}
	 public static Context getAppContext()
	 {
	        return instance;
	 
	 }
	 
	 public static BaseCookie getInstance()
	 {
	        return instance;
	 
	 }
	
	/**
	 * 发送广播--更新应用广播<br>
	 * 2014-2-27 下午1:51:19
	 *
	 */
	public  void sendAppInstall(){
		   Intent myIntent = new Intent(ConstantValue.INSTALL_RECEIVER);  
		   myIntent.putExtra("path", ConstantValue.COMMAND_INSTALL_APP);
	       myIntent.setAction(ConstantValue.INSTALL_RECEIVER);  
	       instance.sendBroadcast(myIntent);  
	}
	
	public void sendCmd(Context context,String action){
		  Intent myIntent = new Intent(action);  
		  myIntent.putExtra("path", ConstantValue.COMMAND_INSTALL_APP);
		  myIntent.setAction(action);  
		  context.sendBroadcast(myIntent);  
	}

	/**
	 * 重启应用<br>
	 * 2014-2-24 上午10:27:51
	 *
	 */
	public  void resoftware(){
		Intent i = getBaseContext().getPackageManager() 
				.getLaunchIntentForPackage(getBaseContext().getPackageName()); 
				i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
				startActivity(i);
	}
	
	/**
	 * 获取存储空间
	 * 2013-11-14 下午3:17:55
	 *
	 */
	public void test1(){
		long in1=StorageUtil.getAvailableInternalMemorySize();
		long in2=StorageUtil.getTotalInternalMemorySize();
		long sd1=StorageUtil.getAvailableExternalMemorySize();
		long sd2=StorageUtil.getTotalExternalMemorySize();
		LogUtil.i(TAG,"in1="+in1);
		LogUtil.i(TAG,"in2="+in2);
		LogUtil.i(TAG,"sd1="+sd1);
		LogUtil.i(TAG,"sd2="+sd2);
		
	}
    /**
     * @return the main resources from the Application
     */
    public static Resources getAppResources()
    {
        if(instance == null) return null;
        return instance.getResources();
    }
    
    public static int getLocalVersionCode()   {  
        PackageManager packageManager = getAppContext().getPackageManager();  
        PackageInfo packageInfo=null;
		try {
			packageInfo = packageManager.getPackageInfo(  
					getAppContext().getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}  
        return packageInfo.versionCode;  
    }  
    
    public static String getRelease(){
    	
    	return Build.VERSION.RELEASE;
    }
}
